package com.protocols.sctp.m3ua.sccp.tcap.camel;

import java.io.Serializable;

import com.protocols.BCDNumber;
import com.protocols.OctaveInteger;
import com.protocols.ProtoOperations;
import com.protocols.ProtocolStackException;
import com.protocols.sctp.m3ua.sccp.tcap.TcapBegin;
import com.protocols.sctp.m3ua.sccp.tcap.TcapConstructor;
import com.protocols.sctp.m3ua.sccp.tcap.TcapPrimitiveUnitByteArray;
import com.protocols.sctp.m3ua.sccp.tcap.TcapPrimitiveUnitNumber;

public class CamelConnect extends CamelInvoke implements Serializable{
	
	private TcapConstructor Parameters = new TcapConstructor(0x30);
	private TcapConstructor DestinationRoutingAddress = new TcapConstructor(0xa0);
	private TcapPrimitiveUnitByteArray DestinationNumber = new TcapPrimitiveUnitByteArray(0x04);
	private TcapPrimitiveUnitByteArray AlteringPatern = new TcapPrimitiveUnitByteArray(0x81);
	private TcapPrimitiveUnitByteArray OriginalCalledPartyID = new TcapPrimitiveUnitByteArray(0x86);
	private TcapPrimitiveUnitByteArray Extensions = new TcapPrimitiveUnitByteArray(0xAA);
	private TcapPrimitiveUnitByteArray CallingPartyCategory = new TcapPrimitiveUnitByteArray(0x9c);
	private TcapPrimitiveUnitByteArray ReDirectingPartyID = new TcapPrimitiveUnitByteArray(0x9D);
	private TcapPrimitiveUnitByteArray ReDirectionInformation = new TcapPrimitiveUnitByteArray(0x9e);
	private TcapPrimitiveUnitByteArray GenericNumber = new TcapPrimitiveUnitByteArray(0xAE);
	private TcapPrimitiveUnitByteArray SuppressionOfAnnouncement = new TcapPrimitiveUnitByteArray(0x9f37);
	private TcapPrimitiveUnitByteArray oCSIApplicable = new TcapPrimitiveUnitByteArray(0x9f38);
	private TcapPrimitiveUnitByteArray naInfo = new TcapPrimitiveUnitByteArray(0xbf39);
	
	public CamelConnect() {
		super();		
		setOpCode(20,true);	// Connect
		super.Invoke.addElement(Parameters);
		Parameters.addElement(DestinationRoutingAddress);
		DestinationRoutingAddress.addElement(DestinationNumber);
		Parameters.addElement(AlteringPatern);
		Parameters.addElement(OriginalCalledPartyID);
		Parameters.addElement(Extensions);
		Parameters.addElement(CallingPartyCategory);
		Parameters.addElement(ReDirectingPartyID);
		Parameters.addElement(ReDirectionInformation);
		Parameters.addElement(GenericNumber);
		Parameters.addElement(SuppressionOfAnnouncement);
		Parameters.addElement(oCSIApplicable);
		Parameters.addElement(naInfo);
	}
		
	
	
	public TcapConstructor getRootComponent(){
		return Invoke;
	}
	
	public int getDestinationNumberInfo() {
		return ProtoOperations.intFromByteArray(DestinationNumber.getTagValue(), 0, 2);
	}
	
	public String getDestinationNumber() {
		
		byte [] val = DestinationNumber.getTagValue();
		
		if (val.length>1){
			int i=2;
			String value="";
			while (i<val.length){
				value+=Integer.toHexString(val[i] & 0b00001111);
				value+=Integer.toHexString((val[i] & 0b11110000)>>4);
				i++;			
			}	
			
			if ((val[0]&0xff)>=80)
			{
				return value.substring(0, value.length()-1);
			}
			return value;
		}			
		
		return "";
	}

	public void setDestinationNumber(int destinationNumberInfo,String destinationNumber) {		
		BCDNumber destination = new BCDNumber(destinationNumber, "f");	
		destinationNumberInfo=destinationNumber.length()%2==0?(destinationNumberInfo&0x7fff):(destinationNumberInfo|0x8000);
		OctaveInteger  destinfo = new OctaveInteger(destinationNumberInfo,2);
		DestinationNumber.setTagValue(ProtoOperations.concatArrays(destinfo.getByteArray(), destination.getByteArray()));
	}
	
	
}
